package org.pearl.app;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/**
 * Created by TD on 2021/11/29
 */
@RestController
@RequestMapping("/app1")
@Api(tags = "测试")
public class App1Controller {


    @GetMapping("/test")
    @ApiOperation(value = "测试接口")
    @ApiOperationSupport(author = "xiaoymin@foxmail.com")
    public String test(String name) {
        return name;
    }

    @GetMapping("/testQueryMap")
    String testQueryMap(Map<String, String> map) {
        return map.toString();
    }


/*
    @PostMapping("/post")
    String post(@RequestBody String user, HttpServletRequest request) {
        DataMap dataMap = HttpUtil.readParameter(request);
        String queryString = request.getQueryString();
        return user;
    }
*/

    @GetMapping("/xss")
    @ApiOperation(value = "xss")
    public String xss(String name) {
        return name;
    }


    @GetMapping("/get")
    @ApiOperation(value = "get")
    public String get(HttpServletRequest request) throws IOException {
        System.out.println("==============");
        String line = request.getReader().readLine();
        ServletInputStream inputStream = request.getInputStream();
        return "name";
    }

    public static String readRequestBody(HttpServletRequest request) {
        BufferedReader br = null;

        try {
            br = request.getReader();
            String line = br.readLine();
            if (line == null) {
                return "";
            } else {
                StringBuilder ret = new StringBuilder();
                ret.append(line);

                while ((line = br.readLine()) != null) {
                    ret.append('\n').append(line);
                }

                return ret.toString();
            }
        } catch (IOException var4) {
            throw new RuntimeException(var4);
        }
    }
}
